﻿using StudentDatabase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XskcDAL
{
    public class StudentDAL
    {
        public List<Student> GetAllStudents()
        {
            try
            {
                using (xskcContext db = new xskcContext())
                {
                    return db.Students.ToList<Student>();
                }
            }
            catch
            {
                return null;
            }
        }
        
        public List<Student> SelectStudentsBySname(String sname)
        {
            try
            {
                using (xskcContext db = new xskcContext())
                {
                    var stu_list = (from s in db.Students
                               where s.Sname == sname
                               select s).ToList<Student>();

                    return (List<Student>)stu_list;
                }
            }
            catch
            {
                return null;
            }
        }
      
        public bool InsertStudent(Student stu)
        {
            try
            {
                using (xskcContext db = new xskcContext())
                {
                    db.Students.Add(stu);
                    db.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
  
        public bool UpdateStudentBySno(String sno, Student uStu)
        {
            try
            {
                using (xskcContext db = new xskcContext())
                {
                    var temp = (from s in db.Students
                                where s.Sno == sno
                                select s).FirstOrDefault();
                    temp.Sname = uStu.Sname;
                    temp.Ssex = uStu.Ssex;
                    
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool DeleteStudentBySno(String sno)
        {
            try
            {
                using (xskcContext db = new xskcContext())
                {
                    var temp = db.Students.Find(new object[] { sno });
                    db.Students.Remove(temp);
                    db.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
